//
//  CYShareView.m
//  cyjf
//
//  Created by tauCross on 2017/8/1.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYShareView.h"



@interface CYShareView ()

@property(nonatomic, strong)UIView *backgroundView;
@property(nonatomic, strong)UIView *contentView;

@end


@implementation CYShareView

- (instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self)
    {
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = HEXCOLORA(@"000000", 0.5);
        self.backgroundView.alpha = 0;
        [self addSubview:self.backgroundView];
        
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 185 + SAFE_BOTTOM_HEIGHT)];
        self.contentView.backgroundColor = HEXCOLOR(@"FFFFFF");
        self.contentView.top = self.height;
        [self addSubview:self.contentView];
        {
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.text = @"分享到";
            titleLabel.font = [UIFont systemFontOfSize:16];
            titleLabel.textColor = HEXCOLOR(@"444444");
            [titleLabel sizeToFit];
            titleLabel.centerX = self.contentView.width / 2;
            titleLabel.centerY = 25;
            [self.contentView addSubview:titleLabel];
            
            UIView *titleLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
            titleLine.width = self.contentView.width;
            titleLine.height = 1 / SCREEN_SCALE;
            titleLine.left = 0;
            titleLine.bottom = 50;
            [self.contentView addSubview:titleLine];
            
            CGSize buttonSize = CGSizeMake((self.contentView.width - 30) / 4, 134);
            UIButton *wechatButton = [self buttonWithImage:CYImage(@"share_wechat") title:@"微信" size:buttonSize];
            [wechatButton addTarget:self action:@selector(wechatAction) forControlEvents:UIControlEventTouchUpInside];
            [wechatButton titleImageCenterStyleWithOffset:10];
            wechatButton.left = 15;
            wechatButton.top = 50;
            [self.contentView addSubview:wechatButton];
            
            UIButton *wechatTimelineButton = [self buttonWithImage:CYImage(@"share_wechat_timeline") title:@"朋友圈" size:buttonSize];
            [wechatTimelineButton addTarget:self action:@selector(wechatTimelineAction) forControlEvents:UIControlEventTouchUpInside];
            [wechatTimelineButton titleImageCenterStyleWithOffset:10];
            wechatTimelineButton.left = wechatButton.right;
            wechatTimelineButton.top = 50;
            [self.contentView addSubview:wechatTimelineButton];
            
            UIButton *qqButton = [self buttonWithImage:CYImage(@"share_qq") title:@"QQ" size:buttonSize];
            [qqButton addTarget:self action:@selector(qqAction) forControlEvents:UIControlEventTouchUpInside];
            [qqButton titleImageCenterStyleWithOffset:10];
            qqButton.left = wechatTimelineButton.right;
            qqButton.top = 50;
            [self.contentView addSubview:qqButton];
            
            UIButton *weiboButton = [self buttonWithImage:CYImage(@"share_weibo") title:@"微博" size:buttonSize];
            [weiboButton addTarget:self action:@selector(weiboAction) forControlEvents:UIControlEventTouchUpInside];
            [weiboButton titleImageCenterStyleWithOffset:10];
            weiboButton.left = qqButton.right;
            weiboButton.top = 50;
            [self.contentView addSubview:weiboButton];
            
            UIView *cancelLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
            cancelLine.width = self.contentView.width;
            cancelLine.height = 1 / SCREEN_SCALE;
            cancelLine.left = 0;
            cancelLine.top = self.contentView.height - SAFE_BOTTOM_HEIGHT - 1;
            [self.contentView addSubview:cancelLine];
            
            UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
            cancelButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            cancelButton.tintColor = HEXCOLOR(@"444444");
            cancelButton.width = self.contentView.width;
            cancelButton.height = 50;
            cancelButton.left = 0;
            cancelButton.top = cancelLine.top;
            [cancelButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:cancelButton];
            
        }
    }
    return self;
}

#pragma mark - Action
- (void)show
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.backgroundView.alpha = 1;
                         self.contentView.bottom = self.height;
                     }
                     completion:^(BOOL finished) {
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)close
{
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.backgroundView.alpha = 0;
                         self.contentView.top = self.height;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)wechatAction
{
    if(self.shareAction)
    {
        self.shareAction(CYShareTargetWechat);
    }
    [self close];
}

- (void)wechatTimelineAction
{
    if(self.shareAction)
    {
        self.shareAction(CYShareTargetWechatTimeLine);
    }
    [self close];
}

- (void)qqAction
{
    if(self.shareAction)
    {
        self.shareAction(CYShareTargetQQ);
    }
    [self close];
}

- (void)weiboAction
{
    if(self.shareAction)
    {
        self.shareAction(CYShareTargetWeibo);
    }
    [self close];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:self];
    if(!CGRectContainsPoint(self.contentView.frame, point))
    {
        [self close];
    }
}

#pragma mark - Private Method
- (UIButton *)buttonWithImage:(UIImage *)image title:(NSString *)title size:(CGSize)size
{
    UIButton *button = [[UIButton alloc] init];
    button.size = size;
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:HEXCOLOR(@"444444") forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    return button;
}

@end
